package com.service.database.config;

import com.service.database.handler.AbstractDatabaseHandler;
import com.service.database.constants.DbConstants;
import com.service.database.constants.JdbcConstants;
import com.service.database.constants.SqlConstants;
import com.service.database.handler.MysqlHandler;
import com.service.database.handler.OracleHandler;
import com.service.database.handler.PostgresHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库枚举
 * @author xianggj
 * @Date 2021/6/4 11:51
 **/
public enum DataBase {

    //mysql
    MYSQL(JdbcConstants.MYSQL_JDBC_PREFIX, DbConstants.MYSQL_DRIVER, SqlConstants.SPECIFIC_PARTITION_MYSQL),
    //oracle
    ORACLE(JdbcConstants.ORACLE_JDBC_PREFIX, DbConstants.ORACLE_DRIVER, SqlConstants.SPECIFIC_PARTITION_ORACLE),
    //pg
    POSTGRESQL(JdbcConstants.PG_JDBC_PREFIX, DbConstants.POSTGRESQL_DRIVER, SqlConstants.SPECIFIC_PARTITION_ORACLE),
    //达梦
//    DM8(JdbcConstants.DM8_JDBC_PREFIX, DbConstants.DAMENG_DRIVER, SqlConstants.SPECIFIC_PARTITION_ORACLE)
    ;
    /**
     * 根据枚举获取处理器
     *
     * @return
     */
    public AbstractDatabaseHandler getHandler(){
        switch (this){
            case ORACLE:
                return new OracleHandler(this);
            case POSTGRESQL:
                return new PostgresHandler(this);
//            case DM8:
//                return new Dm8Handler(this);
            default:
                return new MysqlHandler(this);
        }
    }

    /**
     * jdbc前缀
     */
    private String jdbcPrefix;

    /**
     * 驱动类
     */
    private String driver;

    /**
     * 特殊转义符
     */
    private String transferChar;

    DataBase() {
    }

    DataBase(String jdbcPrefix, String driver, String transferChar) {
        this.jdbcPrefix = jdbcPrefix;
        this.driver = driver;
        this.transferChar = transferChar;
    }

    public String getJdbcPrefix() {
        return jdbcPrefix;
    }

    public String getDriver() {
        return driver;
    }

    public String getTransferChar() {
        return transferChar;
    }

    /**
     * 获取数据库
     * @param type
     * @return
     */
    public static DataBase toDataBase(String type){
        for (DataBase dataBase: values()) {
            if (dataBase.name().equalsIgnoreCase(type)){
                return dataBase;
            }
        }
        return null;
    }
    /**
     * 获取数据库
     * @param driver 驱动
     * @return
     */
    public static DataBase getDataBaseByDriver(String driver){
        for (DataBase dataBase: values()) {
            if (dataBase.getDriver().equalsIgnoreCase(driver)){
                return dataBase;
            }
        }
        return null;
    }

    /**
     * 获取数据库列表
     * @return
     */
    public static List<String> getList(){
        List<String> supportDbTypes = new ArrayList<>();
        for (DataBase dataBase: values()) {
            supportDbTypes.add(dataBase.name());
        }
        return supportDbTypes;
    }
}
